package com.open.party.cloud.view.comm;

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.FragmentActivity
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppBaseFragment
import com.open.party.cloud.model.BizType
import com.open.party.cloud.model.FileEntity
import com.open.party.cloud.model.data.CodeValue
import com.open.party.cloud.viewModel.BaseViewModel
import com.open.party.cloud.viewModel.FileViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.helper.image.compress.ImageCompress
import com.sinothk.image.selector.PhotoPickerActivity
import com.sinothk.image.selector.SelectModel
import com.sinothk.image.selector.intent.PhotoPickerIntent
import com.sinothk.view.image.crop.CropImageMainActivity
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.File

abstract class MineMainBaseFragment : AppBaseFragment() {
    protected var baseViewModel: BaseViewModel? = null
    var fileViewModel: FileViewModel? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        fileViewModel = FileViewModel()
    }

    protected fun updateAvatar(activity: FragmentActivity) {
        val intent = PhotoPickerIntent(activity)
        intent.setSelectModel(SelectModel.SINGLE)//  SelectModel.SINGLE
        intent.setShowCamera(true, activity.application.packageName) // 是否显示拍照， 默认false com.sinothk.living
        startActivityForResult(intent, CodeValue.IMG_SELECT_CODE)
    }

    private var avatarPath = ""

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (data == null) {
            return
        }

        // 图片选择
        if (requestCode == CodeValue.IMG_SELECT_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                val filePathList = data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT)

                if (filePathList != null && filePathList.size > 0) {
                    val imageFilePath = filePathList[0]
                    val oldFile = File(imageFilePath)
                    val fileSize = oldFile.length()

                    val mb = 0.5 * 1024 * 1024
                    if (fileSize > mb) {// 大于0.5MB压缩
                        ImageCompress.execute(imageFilePath) { obj,_ ->
                            if (obj != null) {

                                val newPath: String = obj as String
                                val newFile = File(newPath)
                                if (newFile.length() > 0) {
                                    cutImage(newPath)
                                } else {
                                    XUtils.toast().show("压缩图片为空")
                                }
                            } else {
                                XUtils.toast().show("图片压缩失败")
                            }
                        }
                    } else {
                        if (oldFile.length() > 0) {
                            cutImage(imageFilePath)
                        } else {
                            XUtils.toast().show("选择的文件太小")
                        }
                    }
                } else {
                    XUtils.toast().show("选择图片失败")
                }
            }
            return
        }

        // 图片剪切
        if (requestCode == CodeValue.IMG_CROP_CODE) {
            when (resultCode) {
                -1 -> {
                    avatarPath = data.getStringExtra("cropImagePath") as String
                    doUploadFile(avatarPath)
                }
                -100 -> XUtils.toast().show("取消图片剪切")
                else -> XUtils.toast().show("图片剪切未知错误")
            }
            return
        }
    }

    private fun cutImage(filePath: String) {
        CropImageMainActivity.start(this, filePath, CodeValue.IMG_CROP_CODE, CropImageMainActivity.STYLE_4)
    }

    private fun doUploadFile(filePath: String) {
        showLoadingDialog("正在上传")
        fileViewModel?.uploadFile("", BizType.USER_AVATAR_TYPE, filePath)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun receiveUploadFileEventBus(resultInfo: ResultInfo<FileEntity>) {

        if ("uploadFile" != resultInfo.eventType) {
            return
        }

        hideLoadingDialog()

        when (resultInfo.code) {
            ResultInfo.SUCCESS -> {
                if (resultInfo.data != null) {
                    updateAvatarView(resultInfo.data.url)
                    baseViewModel!!.updateUserPhoto(resultInfo.data.url)
                    val delFlag = File(avatarPath).delete()
                    if (delFlag) {
                    }
                } else {
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(resultInfo.msg)
            }
        }
    }
}
